import mx.events.EventDispatcher;
import mx.utils.Delegate;

import org.molamil.acto.core.EventConstants;
import org.molamil.acto.core.Logger;
import org.molamil.acto.mc.mcw.LcMcWrapper;
import org.molamil.acto.pages.AbstractMovieContext;
import org.molamil.acto.pages.AbstractMovieRequest;
import org.molamil.acto.pages.master.Master;
import org.molamil.acto.pages.master.DependsProvider;
import org.molamil.acto.pages.master.DependsManager;

/**
 * XXX: Document.
 *
 * @author Jorge Hernandez
 */
class org.molamil.acto.pages.master.AbstractMovieMaster implements Master, DependsProvider {
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("AbstractMovieMaster");
	
	private var _context:AbstractMovieContext;
	private var _request:AbstractMovieRequest;
	private var _mcWrapper:LcMcWrapper;
	private var _isDisplayed:Boolean;
		private var dependsManager:DependsManager;
		private var mcStateChangeDelegate:Function;
	
	
	// Constructor
	
	public function AbstractMovieMaster() {
		log.debug("Constructor");
		EventDispatcher.initialize(this);
		_isDisplayed = false;
		dependsManager = new DependsManager();
		mcStateChangeDelegate = Delegate.create(this, mcStateChangeListener);
	}
	
	
	// Functions
	
	public function dispatchEvent(eventObj:Object):Void {}
	public function addEventListener(event:String, handler):Void {}
	public function removeEventListener(event:String, handler):Void {}
	
	public function display():Void {
		log.warning("[Abstract] display() to be implemented.");
	}
	
	public function getContext():AbstractMovieContext {
		return context;
	}
	
	function getRequest():AbstractMovieRequest {
		return request;
	}
	
	public function getMcWrapper():LcMcWrapper {
		return mcWrapper;
	}
	
	public function clear():Void {
		log.warning("[Abstract] clear() to be implemented.");
	}
	
	public function isDisplayed():Boolean {
		return _isDisplayed;
	}

	public function addDepends(name:String):Void {
		dependsManager.addDepends(name);
	}
	
	public function removeDepends(name:String):Void {
		dependsManager.removeDepends(name);
	}
	
	public function existsDepends(name:String):Boolean {
		return dependsManager.existsDepends(name);
	}
	
	public function getDepends():Array {
		return dependsManager.getDepends();
	}

	private function mcStateChangeListener(eventObject:Object):Void {
		
		log.debug("mcStateChangeListener(), state = " + eventObject.state);
		
		var eventObject:Object = {target: this, type: EventConstants.MC_STATE_CHANGE, 
								  state: eventObject.state};
		dispatchEvent(eventObject);
		
	}
	
	
	// Getter/Setter
	
	/**
	 * The context for this movie containing name, props, etc.
	 */
	public function get context():AbstractMovieContext {
		return _context;
	}
	
	public function set context(context:AbstractMovieContext):Void {
		_context = context;
	}
	
	/**
	 * Current request, always null when <code>isActive() == false</code>.
	 */
	public function get request():AbstractMovieRequest {
		return _request;
	}
	
	public function set request(request:AbstractMovieRequest):Void {
		_request = request;
	}
	
	/**
	 * The MovieClip wrapper that will control the <code>MovieClip</code> representing this movie.
	 */
	public function get mcWrapper():LcMcWrapper {
		return _mcWrapper;
	}
	
	public function set mcWrapper(mcWrapper:LcMcWrapper):Void {
		_mcWrapper = mcWrapper;
		_mcWrapper.addEventListener(EventConstants.MC_STATE_CHANGE, mcStateChangeDelegate);
	}
	
	
}